<template>
    <div>
        <el-cascader
            :options="options"
            :props="props"
            collapse-tags
            filterable
            v-model="orgValue"
            @change="changeValue"
            clearable>
        </el-cascader>
    </div>
</template>

<script>
export default {
    name: "deptUser",
    props: {
        value: {
            type: Array,
            default: null
        },
        updateValue: Function
    },
    data() {
        return {
            props: {
                multiple: true,
                // children: 'childList',
                label: 'name',
                value: 'id'
            },
            options: [],
            userIdValue: this.value,
            orgValue: [],

        }
    },
    methods: {
        getData() {
            function constructMap(res, path, map) {
                for (var a of res) {
                    if (a.type == 3) {
                        var array = JSON.parse(JSON.stringify(path))
                        array.push(a.id)
                        map.set(a.id, array)
                    } else {
                        if (a.parentId == null) {
                            path = [a.id]
                            constructMap(a.childList, path, map)
                        } else {
                            var cPath = JSON.parse(JSON.stringify(path))
                            cPath.push(a.id)
                            constructMap(a.childList, cPath, map)
                        }
                    }
                }
            }


            this.$api.getUserList().then(res => {
                this.options = res
                this.orgValue = this.value
                //   var map = new Map()
                //   constructMap(res , [] , map)
                //   if(this.value){
                //     this.orgValue = []
                //     for(var userId of this.value){
                //       this.orgValue.push(map.get(userId))
                //     }
                //   }
            })

        },
        changeValue(val) {
            this.userIdValue = []
            for (var array of val) {
                this.userIdValue.push(array[array.length - 1])
            }
            if (this.updateValue) {
                this.updateValue(this.userIdValue)
            }
        }
    },
    watch: {
        value(newVal, oldVal) {
            this.userIdValue = newVal
            this.getData()
        }
    },
    mounted() {
        this.getData()
    },


}
</script>

<style scoped>

</style>
